import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {AppConfig} from "../../../../app/appConfig";
import {HttpClient} from "@angular/common/http";
import {AppUrl, TIME_OUT} from "../../../../app/app.general";
import {CommonService} from "../../../../app/common.service";

/**
 * 商户确认客户订单界面
 */

@IonicPage()
@Component({
  selector: 'page-confirm-order',
  templateUrl: 'confirm-order.html',
})
export class ConfirmOrderPage {
  private orderNo;
  private orderDetails = {
    orderDetails: [],
    noPrintDetails: [],
    orderNo: '',
    orderDate: '',
    remark: '',
    sumPrice: '',
    tId: 0
  };
  private hasNew: boolean = false;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private appConfig: AppConfig,
              private httpService: HttpClient,
              private cs: CommonService) {
    this.orderNo = this.navParams.get('data');
    if (this.orderNo == '' || typeof(this.orderNo) == undefined) {
      this.appConfig.presentToast("获取参数失败，请重试");
    }
    else {
      this.appConfig.presentLoading('正在获取订单信息');
      this.httpService.post(AppUrl.food.getOrderDetail, this.orderNo).timeout(TIME_OUT).subscribe(data => {
        this.appConfig.loader.dismiss();
        let temp: any = data;
        if (temp.code == 200) {
          this.orderDetails = temp.data;
          if (this.orderDetails.noPrintDetails.length > 0 &&
            this.orderDetails.orderDetails.length > 0)
            this.hasNew = true;
          else if (this.orderDetails.noPrintDetails.length > 0 &&
            this.orderDetails.orderDetails.length === 0)
            this.orderDetails.orderDetails = this.orderDetails.noPrintDetails;
        }
        else
          this.appConfig.presentToast(temp.msg);
      }, e => {
        cs.httpError(e);
        this.appConfig.loader.dismiss();
      })
    }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ConfirmOrderPage');
  }

  confirm_order() {
    this.httpService.post(AppUrl.food.confirmOrder, {
        orderNo: this.orderNo,
        from: "100"
      }
    ).timeout(TIME_OUT).subscribe(_ => {
      let temp: any = _;
      if (temp.code == 200) {
        this.appConfig.presentToast("订单已发送至厨打");
        this.navCtrl.pop();
      }
      else
        this.appConfig.presentToast(temp.msg);
    }, e => this.cs.httpError(e));
  }
}
